<template>
  <div>
    <el-row>
      <el-col :span="6" class="app-container app-container-right">
        <div class="btn-wrapper">
          基本信息
        </div>
        <el-form
          ref="form"
          :model="form"
          label-width="100px"
          :rules="rules"
          :inline="true"
          size="small"
        >
          <el-divider content-position="left">模板组</el-divider>
          <el-form-item label="模板组:" prop="templateGroupId">
            <el-select
              v-model="form.templateGroupId"
              placeholder="请选择模板组"
              filterable
            >
              <el-option
                v-for="(item,index) in templateGroupList"
                :key="index"
                :label="item.name"
                :value="item.id"
                :disabled="item.enabled==0"
              />
            </el-select>
          </el-form-item>
          <el-divider content-position="left">模板参数</el-divider>
          <el-form-item label="包名:" prop="javaPackageName">
             <span slot="label">
                生成包名
                <el-tooltip content="包名一般为域+组织标识+项目，例如com.yanyu.space" placement="top">
                  <i class="el-icon-question"></i>
                </el-tooltip>
             </span>
            <el-input v-model.trim="form.javaPackageName" placeholder="请输入包名"/>
          </el-form-item>
          <el-form-item label="模块名:" prop="javaModuleName">
            <span slot="label">
                生成模块名
                <el-tooltip content="当前模块名与包名共同组成路径, 例如：sys" placement="top">
                  <i class="el-icon-question"></i>
                </el-tooltip>
             </span>
            <el-input v-model.trim="form.javaModuleName" placeholder="请输入模块名"/>
          </el-form-item>
          <el-form-item label="作者:" prop="author">
            <el-input v-model.trim="form.author" placeholder="请输入作者"/>
          </el-form-item>
          <el-divider content-position="left">命名规则</el-divider>
          <el-form-item label="表命名规则" prop="tableNaming">
            <el-radio-group v-model="form.tableNaming">
              <el-radio label="2">驼峰命名</el-radio>
              <el-radio label="1">原样</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="列命名规则" prop="columnNaming">
            <el-radio-group v-model="form.columnNaming">
              <el-radio label="2">驼峰命名</el-radio>
              <el-radio label="1">原样</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="忽略表前缀:" prop="ingoreTablePrefix">
            <el-input v-model.trim="form.ingoreTablePrefix" placeholder="请输入忽略表前缀"/>
          </el-form-item>
          <el-form-item label="忽略表字段:" prop="ignoreColumnNames">
            <el-input v-model.trim="form.ignoreColumnNames" placeholder="请输入忽略表字段"/>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="18" class="app-container" >
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
          <el-form-item label="数据源名称" prop="dsId">
            <el-select v-model="queryParams.dsId" placeholder="请选择数据库" filterable>
              <el-option
                v-for="(item,index) in dsSettingList"
                :key="index"
                :label="item.name"
                :value="item.id"
                :disabled="item.enabled==0"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="app-btn-container">
          <el-col :span="1.5">
            <el-button icon="el-icon-refresh" size="mini" @click="handleGen">生成</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="tables" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="表名" prop="tableName" :show-overflow-tooltip="true"/>
          <el-table-column label="表描述" prop="tableComment" :show-overflow-tooltip="true"/>
        </el-table>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {genCode} from "@/api/codegen/code";
import {listDsSetting, listDsTables} from "@/api/codegen/dsSetting";
import {listTemplateGroup} from "@/api/codegen/templateGroup";

export default {
  name: "CODEGENCODE",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      tableNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 数据源表格数据
      dsSettingList: [],
      templateGroupList: [],
      tables: [],
      // 查询参数
      queryParams: {
        dsId: undefined
      },
      form: {
        templateGroupId: undefined,
        javaPackageName: 'com.yanyu.space',
        javaModuleName: 'sys',
        author: 'yanyu',
        tableNaming: "2",
        columnNaming: "2",
        ingoreTablePrefix: undefined,
        ignoreColumnNames: "create_user,create_time,update_user,update_time,del_flag"
      },
      // 表单校验
      rules: {
        templateGroupId: [
          {required: true, message: "模板组不能为空", trigger: "blur"}
        ],
        javaPackageName: [
          {required: true, message: "生成包名不能为空", trigger: "blur"}
        ],
        javaModuleName: [
          {required: true, message: "生成模块名不能为空", trigger: "blur"}
        ],
        author: [
          {required: true, message: "作者不能为空", trigger: "blur"}
        ],
        tableNaming: [
          {required: true, message: "表命名规则不能为空", trigger: "blur"}
        ],
        columnNaming: [
          {required: true, message: "列命名规则不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.queryParams.pageSize = 5000;
    listDsSetting(this.queryParams).then(result => {
      this.dsSettingList = result.rows

    })
    this.queryParams.pageSize = 5000;
    listTemplateGroup(this.queryParams).then(result => {
      this.templateGroupList = result.rows
    })
  },
  methods: {
    /** 查询数据源列表 */
    getList() {
      if(!this.queryParams.dsId) {
        this.$modal.alert("请选择数据源");
        return ;
      } else {
        this.loading = true;
        listDsTables(this.queryParams.dsId).then(response => {
            this.tables = response;
            this.loading = false;
          }
        );
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        url: undefined,
        username: undefined,
        password: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.tableNames = selection.map(item => item.tableName)
      console.log(this.tableNames)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    handleGen: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = {...this.form}
          params.dsId = this.queryParams.dsId
          params.tableNames = this.tableNames.join(",")
          genCode(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.getList();
             this.loading = false;
          })
        }
      });
    }
  }
};
</script>
